package com.sofaguest.service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sofaguest.dao.MsgDao;
import com.sofaguest.dao.OrderDao;
import com.sofaguest.dao.SofaDao;
import com.sofaguest.entry.MsgInfo;
import com.sofaguest.entry.OrderInfo;
import com.sofaguest.entry.SofaInfo;
import com.sofaguest.entry.SofaResult;
import com.sofaguest.utils.SofaUtils;

/**
 * 订单操作的业务类
 * @author Administrator
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Resource
    private SofaDao sofaDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private MsgDao msgDao;

    @Override
    public SofaResult preOrder(String sofaId, String userId, String custName, String certNum, String linktel,
            String userNumber, String opentime, String endtime) {
        if (SofaUtils.isEmpty(opentime) || SofaUtils.isEmpty(endtime)) {
            return new SofaResult(SofaResult.ERROR_CODE, "入住或离开时间有误,请检查", "");
        }
        String nowDate = SofaUtils.getSysDate();
        opentime = SofaUtils.TransDate(opentime, "yyyy-MM-dd", "yyyyMMddHHmmss");
        endtime = SofaUtils.TransDate(endtime, "yyyy-MM-dd", "yyyyMMddHHmmss");
        if (!SofaUtils.isBig(opentime, nowDate) || !SofaUtils.isBig(endtime, opentime)) {
            return new SofaResult(SofaResult.ERROR_CODE, "入住或离开时间有误,请检查", "");
        }
        List<SofaInfo> sofas = sofaDao.qrySofa(sofaId);
        if ("0".equals(sofas.get(0).getCan_use_tag())) {
            return new SofaResult(SofaResult.ERROR_CODE, "该沙发已被预定,请重新选择", "");
        }
        if (SofaUtils.isBig(userNumber, sofas.get(0).getPerson_num())) {
            return new SofaResult(SofaResult.ERROR_CODE, "入住人数大于该沙发的最大入住数,请重新输入", "");
        }
        if (userId.equals(sofas.get(0).getHost_id())) {
            return new SofaResult(SofaResult.ERROR_CODE, "沙发主人不能预订自己的沙发哦~", "");
        }
        // 计算可入住的时间
        String canUseEndDate = SofaUtils.rollDateStr(opentime, "yyyyMMddHHmmss", Calendar.DAY_OF_MONTH, sofas.get(0)
                .getMax_use_time());
        if (SofaUtils.isBig(endtime, canUseEndDate)) {
            return new SofaResult(SofaResult.ERROR_CODE, "入住总天数大于可住时间,请确认", "");
        }
        String orderId = SofaUtils.createOrderID();
        String hostId = sofas.get(0).getHost_id();
        int orderStatus = 1; // 预定状态
        String orderStartDate = SofaUtils.getSysDate();
        String orderEndDate = "";

        // 入订单
        OrderInfo order = new OrderInfo(orderId, userId, hostId, sofaId, custName, certNum, linktel,
                Integer.valueOf(userNumber), opentime, endtime, orderStatus, orderStartDate, orderEndDate, "", "");
        orderDao.preOrder(order);
        // 修改沙发的可用状态为0-不可用
        Map dataMap = SofaUtils.asMap("canUseTag", "0", "sofaId", sofaId);
        sofaDao.modifySofaStatus(dataMap);
        return new SofaResult(SofaResult.SUCCESS_CODE, "预定成功!请等待主人审批.订单号为:" + orderId + ",订单信息可在个人信息中查询", "");
    }

    @Override
    public SofaResult qryAllOrder(String userId, String userType) {
        List<OrderInfo> orders = orderDao.qryAllOrder(userId);
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", orders);
    }

    @Override
    public SofaResult modifyOrderStatus(int status, String orderId) {
        OrderInfo order = orderDao.qryOrderByOrderId(orderId);
        if (SofaUtils.isEmpty(order)) {
            return new SofaResult(SofaResult.ERROR_CODE, "未查询到订单信息", "");
        }
        // 判断订单是否过期
        if (SofaUtils.isBig(SofaUtils.getSysDate(), order.getStart_date()) && order.getOrder_status() < 3) {
            String invalidReason = "超时未接单";
            String orderEndDate = SofaUtils.getSysDate();
            orderDao.modifyOrderStatus(SofaUtils.asMap("status", 5, "orderId", orderId, "invalidReason", invalidReason,
                    "endDate", orderEndDate));
            // 修改沙发为可入住
            sofaDao.modifySofaStatus(SofaUtils.asMap("canUseTag", "1", "sofaId", order.getSofa_id()));
            return new SofaResult(SofaResult.ERROR_CODE, "该订单信息已过期", "");
        }
        if ((2 == status || 5 == status) && order.getOrder_status() != 1) { // 接单或退单只有在预定状态下
            return new SofaResult(SofaResult.ERROR_CODE, "该订单信息不正确,请重新查看", "");
        }
        if (3 == status) {// 离开
            SofaInfo sofa = sofaDao.qrySofaInfo(order.getSofa_id());
            sofa.setCan_use_tag("1");// 修改沙发状态为可入住
            sofa.setUse_count(sofa.getUse_count() + 1);// 增加使用次数
            sofaDao.addSofaBySofaId(sofa);// 更新沙发的信息
        }
        orderDao.modifyOrderStatus(SofaUtils.asMap("status", status, "orderId", orderId));
        String detail = 2 == status ? "接单成功" : "退单成功";
        return new SofaResult(SofaResult.SUCCESS_CODE, detail, "");
    }

    @Override
    public SofaResult commitOrder(String msg, String orderId) {
        // 入评论
        OrderInfo order = orderDao.qryOrderByOrderId(orderId);
        if (order.getOrder_status() != 3) {
            return new SofaResult(SofaResult.ERROR_CODE, "请勿重复评论", "");
        }
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setMassage_id(SofaUtils.createMassageID());
        msgInfo.setUser_id(order.getUser_id());
        msgInfo.setHost_id(order.getHost_id());
        msgInfo.setSofa_id(order.getSofa_id());
        msgInfo.setUse_start_date(order.getStart_date());
        // 计算使用时间
        int useDate = SofaUtils.daysOfTwo(order.getStart_date(), order.getEnd_date(), "yyyyMMDDHHmmss");
        msgInfo.setUse_date(useDate);
        msgInfo.setMassage(msg);
        msgInfo.setMassage_date(SofaUtils.getSysDate());
        msgDao.addMsgInfo(msgInfo);
        // 修改订单状态
        String orderEndDate = SofaUtils.getSysDate();
        Map orderMap = SofaUtils.asMap("status", 4, "orderId", orderId, "endDate", orderEndDate);
        orderDao.modifyOrderStatus(orderMap);
        return new SofaResult(SofaResult.SUCCESS_CODE, "评论成功", "");
    }

    @Override
    public SofaResult getOrderList(int byTimeTag) {
        List<OrderInfo> orders = orderDao.getOrderList(byTimeTag);
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", orders);
    }
}
